package com.jmall.base.core.security;

import java.util.Map;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;


import com.jmall.base.utils.SecurityUtils;

//定义加密规则
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
	@Override
	public boolean doCredentialsMatch(AuthenticationToken authcToken,
			AuthenticationInfo info) {
		//token的地方就是用户输入的
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		Object accountCredentials = getCredentials(info);
		SimpleAccount acc = (SimpleAccount)info;
		//获得数据库里的User对象
		Map u = (Map)acc.getPrincipals().getPrimaryPrincipal();
		//讲前端输入的用户名\密码加盐匹配
		Object tokenCredentials = SecurityUtils.toMD5Hex(String.valueOf(token.getPassword()) , u.get("salt").toString());
		// 将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
		//tokenCredentials经过加密后的用户输入密码与accountCredentials(数据保存的密文)是否相同,return true代表密码校验通过,return false代表密码校验失败
		return equals(tokenCredentials, accountCredentials);
	}
}